<?php
/**
**2015-10-21 对接山海关切换二次交互模式
**User By  PZP
**/
include('/var/www/html/new/d/module/api/ShanHaiGuan/shanhaiguan.class.php');
include('/var/www/html/new/d/module/api/function.class.php');

$ac='16ucom';
$pw='c33367701511b4f6020ec61ded352059';
$inSide = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));

if (!class_exists("go_sql")){
	include("/var/www/html/new/conf/le.je");
	$le=new go_sql();
	$le->connect();
}

$txt=json_encode($_REQUEST);
logfile($txt,'shg_get_data');

$PFT_infunction = new AllFunction(); 

if(!isset($_REQUEST['Action']) && $_REQUEST['PayStatus'] == 1){	

	if(!$_REQUEST['TicketID']) exit("101");

	$tid = $_REQUEST['TicketID'] + 0; //门票id，根据门票id获取航线及票类
	$select_uuid = "select t.uuid,t.pid,t.title as ttitle,l.title as ltitle,t.landid,l.apply_did,m.account from uu_jq_ticket as t, uu_land as l,pft_member as m where l.id = t.landid and t.id=$tid and l.apply_did=m.id limit 1";
	$GLOBALS['le']->query($select_uuid);
	if(!$GLOBALS['le']->fetch_assoc())	exit("101");		
	$uuid = $GLOBALS['le']->f('uuid');  //查询票类id	
	$pid = $GLOBALS['le']->f('pid');  
	$ltitle = $GLOBALS['le']->f('ltitle'); 
	$ttitle = $GLOBALS['le']->f('ttitle'); 
	$lid = $GLOBALS['le']->f('landid');  
	$apply_did = $GLOBALS['le']->f('apply_did');  
	$account = $GLOBALS['le']->f('account'); 
	
	$shg = new shanhaiguan($lid); 
	
	$get_lsprice = $PFT_infunction->Dynamic_Price_And_Storage($account,$pid,substr($_REQUEST['PlayDate'],0,10),1,$apply_did);
	$get_lsprice_xml = simplexml_load_string($get_lsprice);
	$UUlprice = (string)$get_lsprice_xml->Rec->UUsprice;

	$ls_order = 'pft'.str_replace('.',mt_rand(1000,9999),microtime(true));	
	$request_array = array(
		'UUltitle'		=> $ltitle,
		'UUttitle'		=> $ttitle,
		'UUordertime'	=> date('Y-m-d H:i:s'),
		'pftordernum' 	=> $ls_order,
		'ispay'			=> 1,
		'uuid'			=> $uuid,	
		'pftordername' 	=> $_REQUEST['TouristName'],
		'UUordertel'	=> $_REQUEST['TouristTel'],
		'IDcar'			=> $_REQUEST['IdCard'],
		'UUtnum' 		=> $_REQUEST['Tnum'],
		'UUlprice'		=> $UUlprice,
		'UUplaytime'	=> substr($_REQUEST['PlayDate'],0,10)
	);

	$request = $shg->createOrder($request_array);
	if($request == 101) exit("101|0|0|0|0|0");
	else{			
		exit("200|0|0|0|".$request['ticketCode'].'|'.$request['partnerorderId'].'_'.$ls_order);
	}
	
}elseif($_REQUEST['Action'] == 'Relation'){ //下单成功

	$Ordern = $_REQUEST['Ordern'];
	$pft_order_xml = $PFT_infunction->Select_PFTorder($_REQUEST['Ordern']);
	$tnum = (int)$pft_order_xml->Rec->UUtnum;
	$tordernums = explode('_',(string)$pft_order_xml->Rec->UUtordernum);
	$apiOrder = $tordernums['0'];
	$tempOrder = $tordernums['1'];

	$str="insert into all_api_order (pftOrder,apiOrder,tempOrder,coopB,oStatus,oStnum,apiCode,handleStatus,cTime) values('$Ordern','$apiOrder','$tempOrder',11,0,$tnum,'$apiOrder',0,now())";
	$GLOBALS['le']->query($str);
	
}elseif($_REQUEST['Action'] == 'MOD'){//取消订单
	// if($_REQUEST['Ordern'] ==  '1553521') exit("200");
	$modify_array = array();
	
	$pft_order_xml = $PFT_infunction->Select_PFTorder($_REQUEST['Ordern']);	
	$UUtnum = (int)$pft_order_xml->Rec->UUtnum;
	$UUpid = (int)$pft_order_xml->Rec->UUpid;
	$lid = (int)$pft_order_xml->Rec->UUlid;
	$UUplaytime = (string)$pft_order_xml->Rec->UUbegintime;
	$UUtordernum = explode("_",(string)$pft_order_xml->Rec->UUtordernum);
	$UUaids=(string)$pft_order_xml->Rec->UUaids;
	if($UUaids == 0) $UUaid = (int)$pft_order_xml->Rec->UUaid;
	else{
		$UUaidss= explode(',',$UUaids);
		$UUaid = $UUaidss[0]; 
	}
	$sql="select account from pft_member where id=".$UUaid." limit 1";
	$GLOBALS['le']->query($sql);
	$GLOBALS['le']->fetch_assoc(); 
	$account=$GLOBALS['le']->f('account');

	$get_lsprice = $PFT_infunction->Dynamic_Price_And_Storage($account,$UUpid,$UUplaytime,1,$UUaid);
	$get_lsprice_xml = simplexml_load_string($get_lsprice);
	$UUlprice = (string)$get_lsprice_xml->Rec->UUsprice;

	$rnum = $_REQUEST['Tnum'] == 0 ? 0 : $UUtnum - $_REQUEST['Tnum'];
	
	$modify_array['PFTOrdern'] = $UUtordernum[1];
	$modify_array['orderMoney'] = $UUtnum * $UUlprice ;
	$modify_array['tnum'] = $rnum;	
	
		
	$shg = new shanhaiguan($UUaid); 
	$result = $shg->applyOrderRefundByUser($modify_array);
	
	if($result == 101) exit("1077|0|0|0");
	else{
		$upoStatus = $_REQUEST['Tnum'] == 0 ? 3 : 4;
		$str="update all_api_order set oStatus=$upoStatus,handleStatus=0 where pftOrder='".$_REQUEST['Ordern']."' limit 1";
		$GLOBALS['le']->query($str);
		exit("200|0|0|0");
	}
	
}


// function logfile($txt,$logname){
	// $fp = fopen('/var/www/html/new/d/logs/api/ShanHaiGuan/'.$logname.date('Y-m-d').'.txt',"a");
	// flock($fp, LOCK_EX) ;
	// fwrite($fp,date('Y-m-d H:i:s').'|'.$txt."\n");
	// flock($fp, LOCK_UN);
	// fclose($fp); 
// }

function logfile($word,$path){
	$filepath= '/var/www/html/new/d/logs/api/ShanHaiGuan/'.$path.'.txt';
	if(file_exists($filepath)){
		$word = date("Y-m-d H:i:s")."|".$word;
		$word = mb_convert_encoding($word, 'GBK', 'UTF-8');
		$maxsize  = 1024*1024*10;
		$filesize = filesize($filepath);
		$filetype = end(explode(".",$filepath));
		$filename = substr($filepath,0,strpos($filepath,'.'));
		if($filesize>$maxsize)
		{
			rename($filepath, $filename.'_'.date('YmdHis').'.'.$filetype);
			$handler=fopen($filepath,'w');
			flock($handler, LOCK_EX) ;
			fwrite($handler,"\n".$word);
			flock($handler, LOCK_UN);
			fclose($handler);
		}
		else
		{
			$fp = fopen('/var/www/html/new/d/logs/api/ShanHaiGuan/'.$path.'.txt',"a");
			flock($fp, LOCK_EX);
			fwrite($fp,$word."\n");
			flock($fp, LOCK_UN);
			fclose($fp);
		}
	}else{
		$fp = fopen('/var/www/html/new/d/logs/api/ShanHaiGuan/'.$path.'.txt',"a");
		flock($fp, LOCK_EX);
		fwrite($fp,date('Y-m-d H:i:s')."|".$word."\n");
		flock($fp, LOCK_UN);
		fclose($fp);
	}
}
?>